<template>
  <el-dialog
			title="修改密码"
			:visible.sync="dialogPass"
			width="500px"
      @close="show"
			:modal="false">
			<el-form ref="formregister" :rules="registerrules" :model="register" label-width="80px">
        <el-form-item label="邮箱" prop="email">
          <el-input placeholder="请输入邮箱" v-model="register.email"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="username">
          <el-input placeholder="请输入姓名" type="text" v-model="register.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="newPwd">
          <el-input placeholder="请输入密码" type="password" v-model="register.newPwd"></el-input>
        </el-form-item>
      </el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="show">取 消</el-button>
				<el-button :disabled="disabled" type="primary" @click="clickregister">确 定</el-button>
			</span>
      <input type="file" ref="imgfile" v-show="false"  @change="addimg()"/>
		</el-dialog>
</template>
<script>
export default {
  props:{
    dialogPass:{
      type:Boolean,
      default:false
    }
  },
  created(){},
  data() {
    var validateemail = (rule,value, callback) => {
      let re = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/
        if (value === '') {
          callback(new Error('请输入邮箱'));
        } else if(re.test(value)){
          callback();
        } else {
          callback(new Error('请输入正确的邮箱'));
        }
      };
    var validatepassword = (rule,value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else if(value.length >= 6 && value.length <= 18){
          callback();
        } else {
          callback(new Error('请输入6-18位的密码'));
        }
      };
    return {
      disabled:false,
      register:{
        username:'',
        email:'',
        newPwd:''
      },
      registerrules:{
        newPwd: [
          {required: true,validator: validatepassword, trigger: 'blur' }
        ],
        email: [
          {required: true,validator: validateemail, trigger: 'blur' },
        ],
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
      },
    }
  },
  methods:{
    show(){
      this.$emit('closedialogPassword')
    },
    clickregister(){
      this.$refs['formregister'].validate((valid) => {
          if (valid) {
            this.disabled = true
            let list = this.register
            list.newPwd = this.$md5(this.register.newPwd)
            this.$http.post('/api/updatepwd',this.register).then(res => {
              if(res.status == 0){
                this.$message({
                  message: '修改成功！',
                  type: 'success'
                });
                this.show()
                this.register = {
                  username:'',
                  email:'',
                  newPwd:''
                }
              }else{
                this.$message({
                  message: res.message,
                  type: 'error'
                });
              }
              this.disabled = false
            }).catch(err => {
              this.disabled = false
              this.$message({
                  message: err,
                  type: 'error'
                });
            })
          }
      })
    },
  }
}
</script>
<style scoped>

</style>